﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Allocatin_ComodAllo : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Hidid.Value = Request.Cookies["user"].Values["id"];
            HidWareid.Value = getWareId();
            if (HidWareid.Value != "0")
            {
                ddlWare.Visible = false;
                lblWare.Visible = false;
            }
            initWare();
            initType();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            initData();
        }
    }

    private void initData()
    {
        string sql="select stockId, WarehouseName,c.ComoditiesType,d.Brand,e.ComoditiesName,f.Specifications,j.Units,h.Supplier,a.Comodid,ComodBarcode,Prices,TotalNum,Convert(char(10),storageTime,120)as storTime from CS_stock a join CS_Warehouse b on a.warehouseid=b.WarehouseId join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId order by warehousename";
       

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Hidid.Value;
        zs.Open(sql);
        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sql = "select stockId, WarehouseName,c.ComoditiesType,d.Brand,e.ComoditiesName,f.Specifications,j.Units,h.Supplier,a.Comodid,ComodBarcode,Prices,TotalNum,Convert(char(10),storageTime,120)as storTime from CS_stock a join CS_Warehouse b on a.warehouseid=b.WarehouseId join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId where 1=1 ";

        if (!string.IsNullOrEmpty(txtCode.Text))
        {
            sql += " and a.Comodid='" + Common.FormatParameter(txtCode.Text) + "'";
        }
        if (!string.IsNullOrEmpty(txtBrand.Text))
        {
            sql += " and d.Brand='" + Common.FormatParameter(txtBrand.Text) + "'";
        }
        if (!string.IsNullOrEmpty(txtContent.Text))
        {
            sql += " and e.ComoditiesName='" + Common.FormatParameter(txtContent.Text) + "'";
        }
        if (ddlWare.SelectedIndex != 0)
        {
            sql += " and a.WarehouseId=" + ddlWare.SelectedValue;
        }
        if (ddlType.SelectedIndex!=0)
        {
            sql += " and c.TypeId="+ddlType.SelectedValue;
        }
        if (HidWareid.Value != "0")
        {
            ddlWare.Visible = false;
            lblWare.Visible = false;
            sql += " and a.WarehouseId=" + HidWareid.Value;//5&1&a&s&p&x
        }
        sql += " order by warehousename";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    zs.Open("delete CS_stock  where stockId=" + this.GvData.Rows[i].Cells[0].Text);
                    Response.Write("<script>window.location='StockManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }

    private void initWare()
    {
        ZSql zs = new ZSql();
        zs.Open("select WarehouseId,WarehouseName from CS_Warehouse");
        ddlWare.DataSource = zs.m_table;
        ddlWare.DataTextField = "WarehouseName";
        ddlWare.DataValueField = "WarehouseId";
        ddlWare.DataBind();
        ddlWare.Items.Insert(0, new ListItem("—选择仓库—", "0"));
        zs.Close();
    }

    private void initType() 
    {
        ZSql zs = new ZSql();
        zs.Open("select TypeId,ComoditiesType from ComoditiesType");
        ddlType.DataSource = zs.m_table;
        ddlType.DataTextField = "ComoditiesType";
        ddlType.DataValueField = "TypeId";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("—选择类型—", "0"));
        zs.Close();
    }
    protected void linkbtnSales_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("Alloction.aspx?moduid="+Request["moduid"]+"&id="+id+"&action=allo");
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 13, GvData);
    }
}
